package android.project;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class AndroidProjectLibraryActivity extends Activity
{
	public static final String FOLDER_NAME = "/androidProjectDir";
	
	// Field
	private TextView m_textView;
	private List<File> m_fileList;
	private List<String> m_listOfFileNames;
	private ListView m_listView;
	private Intent m_intent;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle a_savedInstanceState) 
	{
	    super.onCreate(a_savedInstanceState);
	    setContentView(R.layout.library);
	    
	    m_textView = (TextView)findViewById(R.id.library_textView_text);
	    m_listView = (ListView)findViewById(R.id.library_listView_list);
	    
	    m_fileList = new ArrayList<File>();
	    m_listOfFileNames = new ArrayList<String>();
	    
	    m_intent = null;
	    
	    new GetImageListTask().execute();
	}
	
	private void setListAdapter()
	{
		m_textView.setVisibility(View.INVISIBLE);
		m_listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, m_listOfFileNames));
		m_textView.setHeight(0);
		m_listView.setOnItemClickListener(new OnItemClickListener() 
		{
			public void onItemClick(AdapterView<?> a_adapter, View a_view, int a_pos, long a_id) 
			{
				// launch intent putextra the filename on the id pos in the list of names array. In the new activity load that image. 
				Log.d("debug-event", "ID: " + a_id + " View: " + a_view.toString());
				
				int id = (int) a_id;
				
				// Get the filename.
				String fileName = m_listOfFileNames.get(id);
				
				// Create the intent and put the filename as extra.
				m_intent = new Intent(getApplicationContext(), AndroidProjectIndividualImageActivity.class);
				m_intent.putExtra("fileName", fileName);
				
				// Start the new activity.
				startActivity(m_intent);				
			}
		});
	}
	
	private class GetImageListTask extends AsyncTask<Void, Void, Boolean>
	{

		@Override
		protected Boolean doInBackground(Void... params) 
		{
			File folder = new File(Environment.getExternalStorageDirectory() + FOLDER_NAME);
			
			if (folder.isDirectory() == true)
			{
				 File[] tempList = folder.listFiles();
				
				if (tempList != null);
				{
					for (int i = 0; i < tempList.length; i++)
					{
						m_fileList.add(tempList[i]);
						String[] tempSplit = tempList[i].toString().split("/");
						
						int numberOfElements = tempSplit.length - 1; // Minus 1 cause of 0 based arrays.
						
						String temp = tempSplit[numberOfElements];
						
						m_listOfFileNames.add(temp);
					}
				}
			}
			else
			{
				Log.d("debug-event", "No directory was found.");
			}
			
			if (m_listOfFileNames.isEmpty() == false)
			{			
				return true;
			}
			
			return false;
		}
		
		@Override
		protected void onPostExecute(Boolean result) 
		{
			if (result == true)
			{
				Log.d("debug-event", "List of images found, posting list.");
				setListAdapter();
			}
			else
			{
				m_textView.setText("No images found.");
			}
		}
	}

}